package com.zx.onlineeducation.service.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.onlineeducation.common.util.R;
import com.zx.onlineeducation.service.edu.entity.TeacherDO;
import com.zx.onlineeducation.service.edu.entity.vo.TeacherQueryVO;
import com.zx.onlineeducation.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-07
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin // 解决跨域
public class TeacherController {

    /**
     * 讲师服务接口
     */
    @Autowired
    private TeacherService teacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("/all")
    public R findAll() {
        List<TeacherDO> teacherDOList = teacherService.list(null);
        return R.ok().data("items", teacherDOList);
    }

    @ApiOperation("获取讲师分页列表")
    @PostMapping("/page/{current}/{size}")
    public R pageConditionList(
            @ApiParam(value = "当前页")
            @PathVariable("current") Long current,
            @ApiParam(value = "每页显示条数")
            @PathVariable("size") Long size,
            @ApiParam(value = "讲师查询对象")
            @RequestBody TeacherQueryVO teacherQueryVO
            ) {
        if(size == null || size == 0) {
            size = 5L;
        }
        // 执行分页查询
        Page<TeacherDO> page = teacherService.page(current, size, teacherQueryVO);
        // 获取总记录数
        long total = page.getTotal();
        // 获取数据列表
        List<TeacherDO> records = page.getRecords();
        // 返回结果
        return R.ok().data("total", total).data("records", records);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "讲师数据对象")
            @RequestBody TeacherDO teacherDO) {
        boolean save = teacherService.saveTeacher(teacherDO);
        return save ? R.ok() : R.error();
    }

    @ApiOperation("根据ID查询讲师")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        TeacherDO teacherDO = teacherService.getById(id);
        return R.ok().data("teacher", teacherDO);
    }

    @ApiOperation("修改讲师")
    @PutMapping("/{id}")
    public R update(
            @ApiParam(value = "讲师ID", required = true)
            @PathVariable("id") String id,
            @ApiParam(value = "讲师数据对象", required = true)
            @RequestBody TeacherDO teacherDO) {
        teacherDO.setId(id);
        boolean update = teacherService.updateTeacherById(teacherDO);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R remove(
            @ApiParam(value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        boolean result = teacherService.removeTeacherById(id);
        return result ? R.ok() : R.error();
    }
}

